<template>
    <div>
        <div class="head">
            <el-button type="primary" :icon="Plus" v-if="!active" @click="addVisible=true">添加分类</el-button>
            <el-input v-model="search" @blur="init" @keyup.enter="init" placeholder="请输入分类名称搜索"></el-input>
        </div>
        <div class="tabel-head">
            <div>分类名称</div>
            <div>分类预览</div>
            <div>创建时间</div>
            <div>商品数</div>
            <div>排序</div>
            <div>状态</div>
        </div>
        <el-scrollbar max-height="calc(100vh - 340px)">
            <Tree :data="data"/>
        </el-scrollbar>
        <el-dialog v-model="addVisible" width="480" center align-center :show-close="false" @close="reset">
            <template #title>
                <CustomTitle title="新建商品一级分类" />
            </template>
            <el-form :model="form" :rules="rules" ref="formRef" label-width='auto'>
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input placeholder="请填写一级商品分类名称" v-model="form.categoryName"></el-input>
                    <span>为了更好的规范展示请简短名称与行业并统一格式：如“灯饰”</span>
                </el-form-item>
                <el-form-item label="联系电话" prop="mobile">
                    <el-input placeholder="请填写联系电话" v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="分类备注" prop="remark">
                    <el-input type="textarea" placeholder="请填写分类描述备注用于记忆" v-model="form.remark"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="addVisible = false">取消</el-button>
                    <el-button type="primary" @click="audit">提交审核</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { onMounted,ref } from "vue";
import Tree from "./Tree.vue";
import { Plus } from '@element-plus/icons-vue';
import { getList,addClassify } from "@/apis/products/classify";
import CustomTitle from "@/components/CustomTitle/index.vue";
import { ElMessage } from "element-plus";

const search=ref('');
const data=ref([]);
const addVisible=ref(false);
const form=ref({
    categoryName:'',
    remark:''
})

const formRef=ref(null)
const init = async ()=>{
    const res = await getList(search.value);
    data.value = res.data
}
const audit=()=>{
    formRef.value.validate(async (valid)=>{
        if(!valid)return false;
        const res = await addClassify(form.value);
        if(res.code==200){
            ElMessage.success('操作成功');
            init()
            addVisible.value=false;
        }    
    })
   
}
const reset=()=>{
    formRef.value.resetFields();
    form.value={};
}
const mobileVerify=(rule,value,callback)=>{
    const regex=/^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
    if(regex.test(value)){
        callback()
    }else{
        callback(new Error('请输入正确的手机号'))
    }
}
const rules={
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
        { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
    ],
    mobile: [
        { required: true, message: '请输入联系电话', trigger: 'blur' },
        { validator:mobileVerify,trigger: 'blur' }
    ],
    remark: [
        { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
    ],
}
onMounted(()=>init())
</script>

<style lang="scss" scoped>


.tabel-head {
    background: #F2F6FC;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    height: 50px;
    line-height: 50px;
    text-align: center;
    margin-top: 20px;
}
span{
    font-size:12px;
    color:#999;
}
.el-input__wrapper{
    background: #F2F3F5;
}
</style>